package ar.com.spsolutions.splilabrary.view.application.configuration;

import java.util.Arrays;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContextHolder;

import ar.com.spsolutions.splibrary.general.RoleConverterService;
import ar.com.spsolutions.splilabrary.view.application.SPApplication;
import ar.com.spsolutions.splilabrary.view.application.security.UserRolesAuthorizer;
import ar.com.spsolutions.splilabrary.view.pages.Home;

import com.google.code.jqwicket.JQComponentOnBeforeRenderListener;

public class ApplicationConfiguration extends Configurator {

	public ApplicationConfiguration(final SPApplication application) {
		super(application);
	}

	@Override
	protected void initialize(final SPApplication app) {
		app.getApplicationSettings().setPageExpiredErrorPage(Home.class);
		app.getSecuritySettings().setAuthorizationStrategy(
				new UserRolesAuthorizer(app));
		final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
				"admin",
				"admin",
				Arrays.asList(new GrantedAuthority[] {
						new GrantedAuthorityImpl(
								RoleConverterService.ROLE_LIBRARIAN),
						new GrantedAuthorityImpl(RoleConverterService.ROLE_USER) }));
		SecurityContextHolder.getContext().setAuthentication(authentication);
		new UploadConfigurator(app).initialize();
		new MounterConfigurator(app).initialize();
		new MapperConfigurator(app).initialize();
		new ModelConfigurator(app).initialize();
		// new QuartzConfigurator(app).initialize();
		app.getComponentPreOnBeforeRenderListeners().add(
				new JQComponentOnBeforeRenderListener());
		SecurityContextHolder.clearContext();
	}

}
